<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">作品墙</div>
    </nav-bar>
    <scroll class="content"
            ref="scroll"
            :probeType="3"
            :pullUpLoad="true"
            @pullingUp="loadMore"
            @scroll="contentScroll">
      <home-swiper :banners="banners"/>
      <recommend-view :recommends="recommends"/>
      <feature-view/>
      <tab-control class="tab-control" :titles="['流行','新款','精选']"
                   @tabClick="tabClick"
      />
      <goods-list :goods="showGoods"/>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop"/>
  </div>
</template>

<script>

import HomeSwiper from "./childComps/HomeSwiper";
import RecommendView from "./childComps/RecommendView";
import FeatureView from "./childComps/FeatureView";

import NavBar from "components/common/navbar/NavBar";
import TabControl from "../../components/content/tabControl/TabControl";
import GoodsList from "../../components/content/goods/GoodsList";
import Scroll from "components/common/scroll/Scroll"
import BackTop from "components/content/backTop/BackTop";
import $axios from 'axios'

import {
    getHomeMultidata,
    getHomeGoods
} from "network/home";

export default {
    name: "Home",
    components: {
        NavBar,
        HomeSwiper,
        RecommendView,
        FeatureView,
        TabControl,
        GoodsList,
        Scroll,
        BackTop
    },
    data() {
      return {
          isShowBackTop:false,
          currentType:'pop',
          goods:{
            'pop':{
                page:0,
                list:[
                    /*{
                        'img':'https://oscimg.oschina.net/oscnet/up-0d41f3674cb94fb314058c21c9d58dc8877.png',
                        'title':'短袖1',
                        'price':'100￥',
                        'cfav':'287'
                    },{
                        'img':'https://oscimg.oschina.net/oscnet/up-0d41f3674cb94fb314058c21c9d58dc8877.png',
                        'title':'短袖2',
                        'price':'100￥',
                        'cfav':'287'
                    },{
                        'img':'https://oscimg.oschina.net/oscnet/up-0d41f3674cb94fb314058c21c9d58dc8877.png',
                        'title':'短袖3',
                        'price':'100￥',
                        'cfav':'287'
                    }*/
                ]
            },
              'new':{
                  page:0,
                  list:[
                      {
                          'img':'https://oscimg.oschina.net/oscnet/up-0d41f3674cb94fb314058c21c9d58dc8877.png',
                          'title':'小狗1',
                          'price':'100￥',
                          'cfav':'287'
                      },{
                          'img':'https://oscimg.oschina.net/oscnet/up-0d41f3674cb94fb314058c21c9d58dc8877.png',
                          'title':'小狗2',
                          'price':'100￥',
                          'cfav':'287'
                      },{
                          'img':'https://oscimg.oschina.net/oscnet/up-0d41f3674cb94fb314058c21c9d58dc8877.png',
                          'title':'小狗3',
                          'price':'100￥',
                          'cfav':'287'
                      }
                  ]
              },
              'sell':{
                  page:0,
                  list:[
                      {
                          'img':'https://oscimg.oschina.net/oscnet/up-0d41f3674cb94fb314058c21c9d58dc8877.png',
                          'title':'小猫1',
                          'price':'100￥',
                          'cfav':'287'
                      },{
                          'img':'https://oscimg.oschina.net/oscnet/up-0d41f3674cb94fb314058c21c9d58dc8877.png',
                          'title':'小猫2',
                          'price':'100￥',
                          'cfav':'287'
                      },{
                          'img':'https://oscimg.oschina.net/oscnet/up-0d41f3674cb94fb314058c21c9d58dc8877.png',
                          'title':'小猫3',
                          'price':'100￥',
                          'cfav':'287'
                      }
                  ]
              }
          },
          banners:[
              {
                  'title':'信息2',
                  'link':'http://www.taobao.com',
                  'image':'/dog.jpg'
              },
              {
                  'title':'信息1',
                  'link':'http://www.baidu.com',
                  'image':'/kqzl.png'
              },
              {
                  'title':'信息2',
                  'link':'http://www.taobao.com',
                  'image':'/dog.jpg'
              },
              {
                  'title':'信息2',
                  'link':'http://www.taobao.com',
                  'image':'/gryh.png'
              }
          ],
          recommends:[
              {
                  'title':'信息1',
                  'link':'http://www.baidu.com',
                  'image':'/dog.jpg'
              },
              {
                  'title':'信息2',
                  'link':'http://www.taobao.com',
                  'image':'/dog.jpg'
              },
              {
                  'title':'信息1',
                  'link':'http://www.baidu.com',
                  'image':'/dog.jpg'
              },
              {
                  'title':'信息2',
                  'link':'http://www.taobao.com',
                  'image':'/dog.jpg'
              }
          ]
      }
    },
    computed:{
      showGoods(){
          console.log("computed showGoods....")
          return this.goods[this.currentType].list
      }
    },
    created() {
       this.getHomeMultidata();
       this.getHomeGoods_('pop');
       this.getHomeGoods_('new');
       this.getHomeGoods_('sell');

    },
    mounted() {
        const refresh = this.debounce(this.$refs.scroll.refresh,30);
        // 监听item中图片加载完成
        this.$bus.$on('itemImageLoad',()=>{
            refresh();
        })
    },
    methods:{
        //防抖动函数
        debounce(func,delay){
          let timer = null
          return function(...args){
              if(timer){
                  clearTimeout(timer)
              }
              timer = setTimeout(()=>{
                  func.apply(this,args)
              },delay)
          }
        },
        loadMore(){
            console.log("加载更多#");
            this.getHomeGoods_(this.currentType);
            this.$refs.scroll.scroll.refresh();
        },
        contentScroll(position){
            // console.log(position);
            this.isShowBackTop = (-position.y) > 200
        },
        backClick(){
            // console.log("huiotu ");
            this.$refs.scroll.scrollTo(0,0)
        },
        /**
         * 事件监听相关方法
         */
        tabClick(index){
  console.log("监听index"+index)
            switch (index) {
              case 0:
                  this.currentType = 'pop'
                    break;
              case 1:
                  this.currentType = 'new'
                  break;
              case 2:
                  this.currentType = 'sell'
                  break;

            }
        },
        /**
         * 网络请求相关方法
         */
        getHomeMultidata(){
            getHomeMultidata().then(res =>(

                console.log("getHomeMultidata...")
                // this.banners = res,
                // this.recommends = res
            ));
        },
        getHomeGoods_(type){
            console.log("当前类型"+type);
            const page = this.goods[type].page + 1;
            getHomeGoods(type,page).then(res =>(
                console.log(res),
                    this.goods[type].list.push(...res),
                    this.goods[type].page += 1,
                    this.$refs.scroll.finishPullUp()
            )).catch(function (error) {
                console.log(error);
            });
        }
    }
  }
</script>

<style scoped>
  #home{
    padding-top: 44px;
    height: 100vh; /*视口高度*/

  }
  .home-nav {
    background-color: var(--color-tint);
    color: #fff;
    position: fixed;
    left:0px;
    right: 0px;
    top:0px;
    z-index: 9;
  }
  .tab-control{
    position: sticky;
    top:44px;
    z-index: 9;
  }
  .content{
    position:absolute;
    top:44px;
    bottom: 49px;
    left:0;
    right: 0;
    /*background: red;*/
    /*height: 500px;*/
    overflow: hidden;


  }
  /*.content{*/
  /*  height: calc(100% - 93px);*/
  /*  overflow: hidden;*/
  /*  margin-top: 44px;*/
  /*}*/
</style>
